package com.kuaixueit.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaixueit.crm.mapper.CDeptMapper;
import com.kuaixueit.crm.pojo.CDept;
import com.kuaixueit.crm.pojo.ResultVO;
import com.kuaixueit.crm.service.DeptService;

@Service
public class DeptServiceImpl implements DeptService {

	
	@Autowired
	private CDeptMapper deptMapper;
	
	
	@Override
	public void saveDept(CDept dept) {
		Date date = new Date();
		dept.setCreateTime(date);
		dept.setUpdateTime(date);
		deptMapper.insert(dept);
	}


	@Override
	public PageInfo findPage(String deptName,Integer page, Integer rows) {
		//分页插件的使用
		PageHelper.startPage(page, rows);
		//查询
		List<CDept> list = new ArrayList<CDept> ();
		if(StringUtils.isEmpty(deptName)){
			list = deptMapper.findList(null);
		}else{
			list = deptMapper.findList("%"+deptName+"%");
		}
	
		//查询数据交给分页插件，自动查询总数
		PageInfo info = new PageInfo(list);
		System.out.println("总数："+info.getTotal()+" 当前页数据："+info.getList());
		return info;
	}


	@Override
	public void deleteByIds(Integer[] ids) {
		
		for (int i = 0; i < ids.length; i++){
			//假如ids有10个，删除到第六个出错了
			//2，事务回滚，都没有删除成功
			int deleteByPrimaryKey = deptMapper.deleteByPrimaryKey(ids[i]);
		}
		
	}


	@Override
	public ResultVO update(CDept dept) {
		Integer id = dept.getId();
		if(null != id){
			deptMapper.updateByPrimaryKey(dept);
			return new ResultVO("修改成功");
		}
		return new ResultVO("修改失败");
	}

}
